package com.example.train_operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.train_operate.dto.TaskDTO;
import com.example.train_operate.dto.UserDTO;
import com.example.train_operate.entity.Network;
import com.example.train_operate.service.NetworkService;
import com.example.train_operate.mapper.NetworkMapper;
import com.example.train_operate.util.Result;
import com.example.train_operate.util.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.example.train_operate.util.FileUrlConstants.FILE_URL;

/**
* @author 21609
* @description 针对表【tb_network】的数据库操作Service实现
* @createDate 2025-08-13 17:06:39
*/
@Service
public class NetworkServiceImpl extends ServiceImpl<NetworkMapper, Network>
    implements NetworkService{

    @Resource
    private NetworkMapper networkMapper;
    /**
     * 添加网络设备检修任务
     * @param network
     * @return
     */
    @Override
    public Result addNetwork(Network network) {
        try{
            UserDTO user = UserHolder.getUser();
            Integer userId = user.getId();
            network.setUserId(userId);
            String photoUrl = network.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String substring = photoUrl.substring(FILE_URL.length());
                network.setPhotoUrl(substring);
            }
            networkMapper.insert(network);
            QueryWrapper<Network> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId)
                    .orderByDesc("create_time")
                    .last("limit 1");
            Network network1 = networkMapper.selectOne(queryWrapper);
            return Result.ok(network1).message("网络设备检修已完成");
        }catch (Exception e){
            return Result.fail().message("出错了");
        }
    }

    /**
     * 根据id查询网络设备检修任务
     * @param networkId
     * @return
     */
    @Override
    public Result getNetworkById(Integer networkId) {
        try{
            QueryWrapper<Network> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("network_id", networkId)
                    .eq("is_delete",0);
            Network network = networkMapper.selectOne(queryWrapper);
            if(network == null){
                return Result.fail().message("没有此条网络设备检修信息");
            }
            String photoUrl = network.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String url = FILE_URL + photoUrl;
                network.setPhotoUrl(url);
            }
            return Result.ok(network);
        }catch (Exception e){
            return Result.fail();
        }
    }

    /**
     * 修改网络设备检修任务
     * @param network
     * @return
     */
    @Override
    public Result updateNetwork(Network network) {
        try{
            String photoUrl = network.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String substring = photoUrl.substring(FILE_URL.length());
                network.setPhotoUrl(substring);
            }
            networkMapper.updateById(network);
            Network network1 = networkMapper.selectById(network.getNetworkId());
            return Result.ok(network1).message("修改成功");
        }catch (Exception e){
            return Result.fail().message("修改失败");
        }
    }


}




